package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.commit.R;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        //获取密码，加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //比对用户名
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = getOne(wrapper);

        //判断查询结果，为null，则返回登录失败
        if (emp == null) {
            return R.error("该用户不存在");
        }
        //判断密码是否正确
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //判断员工是否禁用
        if (emp.getStatus() == 0) {
            return R.error("没有权限，登录失败");
        }
        //存储到session域中
        request.getSession().setAttribute("employee",emp.getId());
        //返回结果
        return R.success(emp);
    }

    @Override
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @Override
    public R<String> add(HttpServletRequest request,Employee employee) {
        //设置默认密码
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);

//        //设置系统时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //获取session中的id
//        Long attribute = (Long) request.getSession().getAttribute("employee");
//
//        //设置添加人
//        employee.setCreateUser(attribute);
//        employee.setUpdateUser(attribute);

        save(employee);

        return R.success("添加成功");

    }

    @Override
    public R<Page> select(int page , int pageSize,String name) {
        Page ip = new Page(page,pageSize);

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        wrapper.orderByDesc(Employee::getCreateTime);

        page(ip,wrapper);

        return R.success(ip);
    }

    @Override
    public R<String> update(HttpServletRequest request,Employee employee) {

//        Long lo = (Long) request.getSession().getAttribute("employee");
//
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(lo);
        updateById(employee);

        return R.success("成功");
    }

    @Override
    public R<Employee> getByIdd(Long id) {

        Employee byId = getById(id);
        if (byId != null) {

            return R.success(byId);
        }
        return R.error("修改失败");
    }

}
